package com.ojoin.ojoin.eb_spider.service.impl;

import com.ojoin.ojoin.eb_spider.bean.CiMingUserOrder;
import com.ojoin.ojoin.eb_spider.service.QueueService;
import com.ojoin.trade.common.utils.LogUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * Created by liaowuhen on 2017/3/22.
 */
@Service
public abstract class QueueServiceImpl<T> implements QueueService<T> {
    private static Logger log = LoggerFactory.getLogger(QueueServiceImpl.class);

    @Override
    // 消费消息，从篮子中取走
    public T consume() throws InterruptedException {
        T co =getQueue().take();
        LogUtils.info(log,this.getClass()+"consume()"+getQueue().size());
        // take方法取出一个苹果，若basket为空，等到basket有消息为止(获取并移除此队列的头部)
        return co;
    }

    @Override
    // 生产消息，放入篮子
    public void produce(T msg) throws InterruptedException {
        // put方法放入一个苹果，若basket满了，等到basket有位置
        if (null != msg) {
            getQueue().put(msg);
            LogUtils.info(log,this.getClass()+"produce()"+getQueue().size());
        }
    }
    @Override
    // 生产消息，放入篮子
    public boolean isEmpt() throws InterruptedException {
        if(getQueue().size() == 0){
            return true;
        }
        return false;
    }

}
